//
//  EnvironmentHeaderView.m
//  Rail
//
//  Created by hychen on 2018/3/6.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "EnvironmentHeaderView.h"
#import "BMItemView.h"

@implementation EnvironmentHeaderModel

@end

@interface EnvironmentHeaderView ()
@property (nonatomic, strong) UIImageView *bgImage;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *monthLabel;
@property (nonatomic, strong) UILabel *tempretatureLabel;
@property (nonatomic, strong) UIView *dataContainer;
@property (nonatomic, strong) BMLineView *lineView;
@property (nonatomic, strong) NSMutableArray *itemArray;
@end
@implementation EnvironmentHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self tlSetupSubviews];
    }
    return self;
}

- (void)tlSetupSubviews {
    self.bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"app_environment_weather_bg_img"]];
    [self addSubview:self.bgImage];
    
    self.lineView = [BMLineView defaultLineView];
    [self addSubview:self.lineView];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(10);
    }];
    
    [self.bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.bottom.equalTo(self.lineView.mas_top);
    }];
    
    self.titleLabel = [UILabel labelWithText:@"" textColor:kWhiteColor font:[UIFont systemFontOfSize:20]];
    [self addSubview:self.titleLabel];
    
    self.monthLabel = [UILabel labelWithText:@" " textColor:kWhiteColor font:[UIFont systemFontOfSize:10]];
    [self addSubview:self.monthLabel];
    
    self.tempretatureLabel = [UILabel labelWithText:@"环境监测" textColor:kWhiteColor font:[UIFont systemFontOfSize:40]];
    [self addSubview:self.tempretatureLabel];
    
    self.dataContainer = [UIView new];
    [self addSubview:self.dataContainer];
    self.dataContainer.backgroundColor = [UIColor clearColor];
 
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.left.mas_equalTo(45);
        make.centerX.equalTo(self);
    }];
    
    [self.monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(25);
        make.centerX.equalTo(self);
    }];
    
    [self.tempretatureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.monthLabel.mas_bottom).offset(5);
        make.centerX.equalTo(self);
    }];

    [self.dataContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.bottom.equalTo(self.lineView.mas_top);
        make.height.mas_equalTo(65);
    }];
}

- (void)setModel:(EnvironmentHeaderModel *)model {
    _model = model;
//    self.titleLabel.text = [NSString stringWithFormat:@"%@环境监测",model.title];
    self.monthLabel.text = model.dataModel.record_time?[model.dataModel.record_time substringToIndex:19]?:@"":@"-";
    self.tempretatureLabel.text = [NSString stringWithFormat:@"%@℃",model.dataModel.temperature?:@"-"];
    NSArray *array = @[
                       @{@"title":@"湿度",@"content":model.dataModel.wetness?:@"-"},
                       @{@"title":@"PM10",@"content":model.dataModel.pm10?:@"-"},
                       @{@"title":@"PM2.5",@"content":model.dataModel.pm?:@"-"},
                       @{@"title":@"噪音",@"content":model.dataModel.noise?:@"-"},
                       @{@"title":@"风力",@"content":model.dataModel.ws?:@"-"},
                       ];
    
    [self setupDataItem:array];
}

- (void)setupDataItem:(NSArray *)dataArray {
    if (dataArray.count < 1) {
        return;
    }
    [self.dataContainer removeAllSubviews];
    self.itemArray = [NSMutableArray array];
    UIView *lastView = nil;
    for (int i = 0; i < dataArray.count; ++i) {
        NSDictionary *dict = [dataArray objectAtIndexSafe:i];
        BMItemView *itemView = [[BMItemView alloc] init];
        itemView.titleLabel.textColor = kWhiteColor;
        itemView.contentLabel.textColor = kWhiteColor;
        itemView.titleLabel.font = [UIFont systemFontOfSize:16];
        itemView.contentLabel.font = [UIFont systemFontOfSize:16];
        [self.itemArray addObject:itemView];
        [self.dataContainer addSubview:itemView];
        [itemView setTitle:[dict objectForKey:@"title"] content:[dict objectForKey:@"content"]];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.equalTo(self.dataContainer);
            }
            else {
                make.left.equalTo(lastView.mas_right);
                make.width.equalTo(lastView);
            }
            make.top.bottom.equalTo(self.dataContainer);
            
            if (i == dataArray.count-1) {
                make.right.equalTo(self.dataContainer);
            }
        }];
        
        lastView = itemView;
    }
}
@end
